package com.wenhai.xiaomage.spring.ioc.overview.dependency.domain;

import com.wenhai.xiaomage.spring.ioc.overview.dependency.enums.City;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Properties;

public class User implements BeanNameAware, AutoCloseable, DisposableBean,InitializingBean {

    private transient String beanName;
    private Long id;
    private String username;
    private City city;
    private Properties context;
    private Company company;

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public User() {
    }

    public User(Long id) {
        this.id = id;
    }

    public String getContextAsText() {
        return contextAsText;
    }

    public void setContextAsText(String contextAsText) {
        this.contextAsText = contextAsText;
    }

    private String contextAsText;

    @Override
    public String toString() {
        return "User{" +
                "beanName='" + beanName + '\'' +
                ", id=" + id +
                ", username='" + username + '\'' +
                ", city=" + city +
                ", context=" + context +
                ", company=" + company +
                ", contextAsText='" + contextAsText + '\'' +
                '}';
    }

    public Properties getContext() {
        return context;
    }

    public void setContext(Properties context) {
        this.context = context;
    }

    public City getCity() {
        return city;
    }

    public String getBeanName() {
        return beanName;
    }

    public void setCity(City city) {
        this.city = city;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }



    @PostConstruct
    public void init() {
        System.out.println("@PostConstruct User bean [" + beanName + "]被初始化了");
    }

    @PreDestroy
    public void preDestroy() {
        System.out.println("@PreDestroy User bean [" + beanName + "]preDestroy 销毁中");
    }

    public static User createUser() {
        User user = new User(1L);
        user.setId(System.currentTimeMillis());
        user.setUsername("createUser");
        return user;
    }

    @Override
    public void setBeanName(String name) {
        this.beanName = name;
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("User bean [" + beanName + "] destroy 销毁中");
    }

    public void customDestroy() {
        System.out.println("User bean [" + beanName + "] customDestroy 销毁中");
    }

    @Override
    public void close() throws Exception {
        System.out.println("User bean [" + beanName + "] close 销毁中");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet User bean [" + beanName + "]被初始化了");
    }
}
